package com.augurit.wf.client.opinion.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.wf.client.opinion.convert.WfUserOpinionConvertor;
import com.augurit.wf.client.opinion.service.IWfUserOpinionService;
import com.augurit.wf.client.opinion.web.form.WfUserOpinionForm;
import com.augurit.wf.common.dao.WfUserOpinionDao;
import com.augurit.wf.common.entity.WfUserOpinion;

@Service
@Transactional
public class WfUserOpinionServiceImpl implements IWfUserOpinionService{

	@Autowired
	private WfUserOpinionDao wfUserOpinionDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WfUserOpinionForm get(Long id) {
		WfUserOpinion entity = wfUserOpinionDao.get(id);
		return WfUserOpinionConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		wfUserOpinionDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WfUserOpinionForm... forms) {
		if(forms != null)
			for(WfUserOpinionForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WfUserOpinionForm form){
		
		if(form != null){
			WfUserOpinion entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = wfUserOpinionDao.get(form.getId());
			}else{
				entity = new WfUserOpinion();
			}
			
			//属性值转换
			WfUserOpinionConvertor.convertFormToVo(form, entity);
			
			//保存
			wfUserOpinionDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}
	
	
	/**
	 * 根据登陆用户id获取常用审批意见
	 * @param userId
	 * @return form对象列表
	 */
	public List<WfUserOpinionForm> getByUserId(Long userId){
		return  this.wfUserOpinionDao.getByUserId(userId);
	}
	
	/**
	 * 清空个人的所有常用意见
	 * @param userId
	 * @return
	 */
	public void deleteAllByUserId(Long userId){
		if(userId != null){
			List<WfUserOpinionForm> list = getByUserId(userId);
			if(list != null && list.size()>0){
				for(WfUserOpinionForm form:list)
					this.delete(form.getId());
			}
		}
	}
	
	public Page<WfUserOpinionForm> search(Page<WfUserOpinionForm> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<WfUserOpinionForm> search(Page<WfUserOpinionForm> page,
			WfUserOpinionForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
